package org.gesplan.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/**
 *
 * @author David
 */
public class ReportManager {

    private static Logger logger;


    public static void execReport(String report) {

        logger = Utils.getLogger();
        try {
            Class.forName("org.postgresql.Driver");
            Connection conn = Utils.getConnection();
            logger.info(Utils.jaspersFolder + report);
            logger.info(Utils.reportsFolder + report);   
            (new File(Utils.reportsFolder)).mkdirs();            
            JasperPrint jasperPrint = JasperFillManager.fillReport(Utils.jaspersFolder + report + ".jasper", null, conn);
            OutputStream output = new FileOutputStream(new File(Utils.reportsFolder + report + ".pdf"));
            JasperExportManager.exportReportToPdfStream(jasperPrint, output);
            output.close();
            Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + Utils.reportsFolder + report + ".pdf");
            p.waitFor();
        } catch (ClassNotFoundException | InterruptedException | IOException | JRException ex) {
            logger.severe(ex.getMessage());
        }

    }
}